package com.vgemv.jsutilitysdkdemo.module.Menu.FilmList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestDictionaryCallback;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.JsThread.JsThreadManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewRefreshCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestManager;
import com.vgemv.jsutilitysdkdemo.R;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FilmListActivity extends AppCompatActivity {


    View mv_listContainer;
    private String defaultOutput = null;
    private String defaultOutputThumb = null;
    private static final int REQUEST_UGC = 1;
    private static final int ChooseFilm = 2;
    private static String UploadedFilmKey = "UploadedFilmKey";
    private static String SharedPreferenceKey = "SharedPreferenceKey";
    private JsMenuFragment mtv_current;
    private JsMenuFragment mtv_checked;
    private JsMenuFragment mtv_checking;
    private JsMenuFragment mtv_unpassed;

    private ArrayList<JsMenuItemVm> marr_checked = new ArrayList();
    private ArrayList<JsMenuItemVm> marr_checking = new ArrayList();
    private ArrayList<JsMenuItemVm> marr_unpassed = new ArrayList();
    private ArrayList<JsMenuItemVm> marr_current = marr_checked;

    private Gson mGson = new Gson();

    private JsMenuFragment mMenu_group;
    private ViewGroup mv_groupContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_uploaded_film);


        mv_groupContainer = findViewById(R.id.mv_groupContainer);

        //设置group按钮
        mMenu_group = new JsMenuFragment();
        mMenu_group.moveToParentView(this,mv_groupContainer);
        mMenu_group.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                return null;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm vm) {

            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                return null;
            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                return null;
            }
        };

        mMenu_group.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                Size deviceSize = JsGeometryManager.getScreenSizeInPixel();

                // unselected
                JsMenuItemStyleDefault unselectedStyle = new JsMenuItemStyleDefault();
                unselectedStyle.mi_width = deviceSize.getWidth()/3;

                int itemHeight = JsGeometryManager.pixelFromDp(60);
                unselectedStyle.mi_height = itemHeight;

                //TODO: 为什么50dp就不显示item了,60才会显示.
                unselectedStyle.mI_backgroundColor = Color.parseColor("#ff000000");
                unselectedStyle.mI_titleTextColor = Color.parseColor("#ffffffff");
                unselectedStyle.mi_titleFontSize = 15;

                JsMenuItemStyleDefault selectedStyle = unselectedStyle.copy();
                selectedStyle.mI_backgroundColor = Color.parseColor("#ff000000");
                selectedStyle.mI_titleTextColor = Color.parseColor("#ffff0000");
                selectedStyle.mi_titleFontSize = 20;


                ArrayList al = new ArrayList();


                JsMenuItemVmDefault model0 = new JsMenuItemVmDefault("我的作品");
                model0.setSelectedStyle(selectedStyle);
                model0.setUnselectedStyle(unselectedStyle);

                JsMenuItemVmDefault model1 = new JsMenuItemVmDefault("审核中");
                model1.setSelectedStyle(selectedStyle);
                model1.setUnselectedStyle(unselectedStyle);

                JsMenuItemVmDefault model2 = new JsMenuItemVmDefault("审核不通过");
                model2.setSelectedStyle(selectedStyle);
                model2.setUnselectedStyle(unselectedStyle);

                al.add(model0);
                al.add(model1);
                al.add(model2);

                return al;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragment, int i, JsMenuItemVm vm) {
                Log.v("info", String.format("Click index at %d",i));
                //TODO: 获取当前已经选择的.
                if(fragment.selectedIndex() == i) {
                    return;
                }

                switch (i) {
                    case 0:
                        mtv_current = mtv_checked;
                        marr_current = marr_checked;
                        break;
                    case 1:
                        mtv_current = mtv_checking;
                        marr_current = marr_checking;
                        break;
                    case 2:
                        mtv_current = mtv_unpassed;
                        marr_current = marr_unpassed;
                        break;
                }

//                FragmentManager fragmentManager = FilmListActivity.this.getSupportFragmentManager();
//                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//                fragmentTransaction.hide(mtv_checked);
//                fragmentTransaction.hide(mtv_checking);
//                fragmentTransaction.hide(mtv_unpassed);
//                fragmentTransaction.show(mtv_current);
//                fragmentTransaction.commit();
//
//                if(marr_current.size() == 0) {
//                    reqeustFirstPageForIndex(i);
//                }
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                JsMenuViewVm menuViewVm = new JsMenuViewVm();
                menuViewVm.mi_layoutStyle = 1;
                menuViewVm.mI_initialSelectedIndex = 0;
                menuViewVm.mb_enableRefreshAndLoadmore = true;
                return menuViewVm;
            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                return null;
            }

            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }
        };



        //设置3个页面列表
        mv_listContainer = findViewById(R.id.mv_listContainer);
        //我的作品
        mtv_checked = new JsMenuFragment();
        mtv_checked.moveToParentView(this, mv_listContainer);
        mtv_checked.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                return marr_checked;

            }

            @Override
            public void jsMenuViewClickItem(JsMenuView menuView, int i, JsMenuItemVm vm) {

            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView menuView, Context context) {
                FilmCell cell = new FilmCell(context);
                cell.mClickCallback = new FilmCellClickCallback() {
                    @Override
                    public void onClickItemAtInex(int index, FilmModel model, int action) {
                        Log.d("xxx","xxx");
                    }
                };
                return cell;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView menuView) {

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView menuView) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_layoutStyle = 2;
                vm.mb_enableRefreshAndLoadmore = true;
                return vm;
            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView menuView, int index) {
                return null;
            }
        };
        mtv_checked.mRefreshCallback = new JsMenuViewRefreshCallback() {
            @Override
            public void onRefresh(JsMenuView fragment) {
                reqeustFirstPageForIndex(0);
            }

            @Override
            public void onLoadMore(JsMenuView fragment, int loadedPage) {
                reqeustLoadNextPageForIndex(0,mtv_checked.loadedPage()+1);
            }
        };







//       //审核中
//        mtv_checking = new GajTableView();
//        mtv_checking.setEnableRefresh(true,true);
//        mtv_checking.mCallback = new GajTableViewCallback() {
//            @Override
//            public ArrayList<FilmModel> onSetDatasource() {
//                return marr_checking;
//            }
//
//            @Override
//            public void onClickItemAtIndex(int i, Object model) {
//
//            }
//
//            @Override
//            public void onLoadMore(int loadedPages) {
//                reqeustLoadNextPageForIndex(1,++mtv_checking.mi_loadedPages);
//            }
//
//            @Override
//            public void onRefresh() {
//                reqeustFirstPageForIndex(1);
//            }
//        };
//        FilmAdapter adpater_checking = new FilmAdapter();
//        adpater_checking.models = marr_checking;
//        mtv_checking.setAdapter(adpater_checking);
//        adpater_checking.mClickCallback = new FilmCellClickCallback() {
//            @Override
//            public void onClickItemAtInex(int index, FilmModel model, int action) {
//                switch (action) {
//                    case FilmCell.ActionDelete: {
//                        Log.d("xxx", "Delete");
//                        GajAlertManager.showNormalDialog(UploadedFilmActivity.this, "温馨提示", "是否删除数据", new GajNormalDialogCallback() {
//                            @Override
//                            public void onClickOk() {
//                                Log.d("xxx","确认删除");
//                                deleteFilm(mtv_checking,marr_checking,model,index);
//                            }
//
//                            @Override
//                            public void onClickCancel() {
//                                Log.d("xxx","取消删除");
//                            }
//                        });
//                        break;
//                    }
//                    case FilmCell.ActionSelect: {
//                        Log.d("xxx", "Select");
//                        Intent intent = new Intent(UploadedFilmActivity.this, PlayActivity.class);
//                        intent.putExtra("filePath",model.moviePath );
//                        startActivity(intent);
//                        break;
//                    }
//                };
//            }
//        };
//
//          //我的作品
//        mtv_unpassed = new GajTableView();
//        mtv_unpassed.setEnableRefresh(true,true);
//        mtv_unpassed.mCallback = new GajTableViewCallback() {
//            @Override
//            public ArrayList<FilmModel> onSetDatasource() {
//                return marr_unpassed;
//            }
//
//            @Override
//            public void onClickItemAtIndex(int i, Object model) {
//
//            }
//
//            @Override
//            public void onLoadMore(int loadedPages) {
//                reqeustLoadNextPageForIndex(2,++mtv_checked.mi_loadedPages);
//            }
//
//            @Override
//            public void onRefresh() {
//                reqeustFirstPageForIndex(2);
//            }
//        };
//        FilmAdapter adpater_unpassed = new FilmAdapter();
//        adpater_unpassed.models = marr_unpassed;
//        mtv_unpassed.setAdapter(adpater_unpassed);
//        adpater_unpassed.mClickCallback = new FilmCellClickCallback() {
//            @Override
//            public void onClickItemAtInex(int index, FilmModel model, int action) {
//                switch (action) {
//                    case FilmCell.ActionDelete: {
//                        Log.d("xxx", "Delete");
//                        GajAlertManager.showNormalDialog(UploadedFilmActivity.this, "温馨提示", "是否删除数据", new GajNormalDialogCallback() {
//                            @Override
//                            public void onClickOk() {
//                                Log.d("xxx","确认删除");
//                                deleteFilm(mtv_unpassed,marr_unpassed,model,index);
//                            }
//
//                            @Override
//                            public void onClickCancel() {
//                                Log.d("xxx","取消删除");
//                            }
//                        });
//                        break;
//                    }
//                    case FilmCell.ActionSelect: {
//                        Log.d("xxx", "Select");
//                        Intent intent = new Intent(UploadedFilmActivity.this, PlayActivity.class);
//                        intent.putExtra("filePath",model.moviePath );
//                        startActivity(intent);
//                        break;
//                    }
//                };
//            }
//        };

        //初始显示已通过的.
//        FragmentManager fragmentManager = FilmListActivity.this.getSupportFragmentManager();
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.add(mv_listContainer.getId(),mtv_checked);
////        fragmentTransaction.add(mv_listContainer.getId(),mtv_checking);
////        fragmentTransaction.add(mv_listContainer.getId(),mtv_unpassed);
////        fragmentTransaction.hide(mtv_checked);
////        fragmentTransaction.hide(mtv_checking);
////        fragmentTransaction.hide(mtv_unpassed);
//        fragmentTransaction.show(mtv_checked);
//        fragmentTransaction.commit();
        reqeustFirstPageForIndex(0);
    }

    //删除视频
    public void deleteFilm(JsMenuView tableView, ArrayList al, FilmModel film, int index) {
        //调用接口
        Map dic_param = new HashMap();
        dic_param.put("id",film.id);
        dic_param.put("page",1);

//        String str_url = String.format("%s/%s",GlobalModel.sharedInstance.curentServerAddress,"material_cms/api/ugc/material/del.action");

//        GajOhNetworkManager.sharedInstance().postRequestWithJsonBody(str_url,dic_param , new IGajNetworkDictionaryCallback() {
//            @Override
//            public void onRequestResponse(Boolean isSuccess, int resultCode, Map result, String detailInfo, Exception e) {
//                JsThreadManager.runInMainThread(0, true, new Runnable() {
//                    @Override
//                    public void run() {
//                        if(isSuccess) {
//                            Integer resultCode =  (Integer) (result.get("result"));
//                            if(resultCode == 0) {
//                                //需要先通知删除再删除. //TODO:这里用notifyItemRemoved会报错.
//                                try {
//                                    al.remove(film);
////                                    tableView.adapter().notifyItemRemoved(index+1);
//                                    tableView.adapter().notifyDataSetChanged();
//
//                                }
//                                catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                Toast.makeText(UploadedFilmActivity.this, "删除成功", Toast.LENGTH_LONG).show();
//                                return;
//                            }
//                        }
//                        Toast.makeText(UploadedFilmActivity.this, "请求失败,请检查网络", Toast.LENGTH_LONG).show();
//                    }
//                });
//            }
//        });
    }



    public void reqeustFirstPageForIndex(int index) {
        String str_url = "https://169.vgemv.com:59091/material_cms/api/ugc/material/list.action";
        Integer state = 1;
        ArrayList marr_operating = null;
        JsMenuFragment mtv_operating = null;
        switch (index) {
            case 0:{
                //我的作品
                state = 1;
                mtv_operating = mtv_checked;
                marr_operating = marr_checked;
                break;
            }
            case 1:{
                //审核中
                state = 0;
                mtv_operating = mtv_checking;
                marr_operating = marr_checking;
                break;
            }
            case 2: {
                //审核不通过
                state = -1;
                mtv_operating = mtv_unpassed;
                marr_operating = marr_unpassed;
                break;
            }
            default:
                break;
        }

        Map dic_param = new HashMap();
        dic_param.put("state",state);
        dic_param.put("page",1);

        final ArrayList<FilmVm> fmarr_operating = marr_operating;
        final JsMenuFragment fmtv_operating = mtv_operating;
        final int fstate = state;

        JsRequestManager.sharedInstance().postReqeustByJson(str_url, dic_param, new JsRequestDictionaryCallback() {
            @Override
            public void onRequestResponse(final int responseCode, final Map resultObject, String detailInfo) {
                JsThreadManager.runInMainThread(0, true, new Runnable() {
                    @Override
                    public void run() {
                        if(responseCode == 200) {
                            Integer resultCode =  ((Integer)resultObject.get("result"));
                            if(resultCode == 0) {
                                fmarr_operating.clear();
                                ArrayList<Map> arr = (ArrayList) resultObject.get("list");
                                if(arr.size() > 0 ) {
                                    for(Map itemMap : arr) {
                                        FilmModel film = new FilmModel();
                                        film.id = String.format("%d",itemMap.get("id"));
                                        film.title = (String)itemMap.get("title");
                                        film.authorName = (String)itemMap.get("author");
                                        film.moviePath = (String)itemMap.get("fileUrl");
                                        film.imagePath = (String)itemMap.get("posterUrl");
                                        film.isRemote = true;
                                        film.checkResult = (String)itemMap.get("auditInfo");
                                        film.checkTime = (String)itemMap.get("auditTime");
                                        if(fstate == -1) {
                                            film.isPassed = false;
                                        }
                                        else {
                                            film.isPassed = true;
                                        }

                                        FilmVm vm = new FilmVm();
                                        vm.setModel(film);
                                        JsMenuItemStyleDefault style = new JsMenuItemStyleDefault();
                                        style.mI_borderWidth = 0;
                                        vm.setStyle(style);
                                        fmarr_operating.add(vm);

                                    }
                                    fmtv_operating.reloadData();
                                    fmtv_operating.finishRefresh(true,true
                                    );
                                }
                                else {
                                    fmtv_operating.finishRefresh(true,false);
                                }
                                return;
                            }
                        }
                        //请求失败.
                        fmtv_operating.finishRefresh(false,false);
                        Toast.makeText(FilmListActivity.this, "请求失败,请检查网络", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

    }

    public void reqeustLoadNextPageForIndex(int index , int pageNum) {
//        Gson gson = new Gson();
//
//        String str_url = String.format("%s/%s",GlobalModel.sharedInstance.curentServerAddress,"material_cms/api/ugc/material/list.action");
//        Integer state = 1;
//        ArrayList marr_operating = null;
//        GajTableView mtv_operating = null;
//        switch (index) {
//            case 0:{
//                //我的作品
//                state = 1;
//                mtv_operating = mtv_checked;
//                marr_operating = marr_checked;
//                break;
//            }
//            case 1:{
//                //审核中
//                state = 0;
//                mtv_operating = mtv_checking;
//                marr_operating = marr_checking;
//                break;
//            }
//            case 2: {
//                //审核不通过
//                state = -1;
//                mtv_operating = mtv_unpassed;
//                marr_operating = marr_unpassed;
//                break;
//            }
//            default:
//                break;
//        }
//
//
//        Map dic_param = new HashMap();
//        dic_param.put("state",state);
//        dic_param.put("page",pageNum);
//
//        final ArrayList fmarr_operating = marr_operating;
//        final GajTableView fmtv_operating = mtv_operating;
//        final int fstate = state;
//        GajOhNetworkManager.sharedInstance().postRequestWithJsonBody(str_url,dic_param , new IGajNetworkDictionaryCallback() {
//            @Override
//            public void onRequestResponse(Boolean isSuccess, int resultCode, Map result, String detailInfo, Exception e) {
//                JsThreadManager.runInMainThread(0, true, new Runnable() {
//                    @Override
//                    public void run() {
//                        if(isSuccess) {
//                            Integer resultCode =  ((Integer)result.get("result"));
//                            if(resultCode == 0) {
//                                ArrayList<Map> arr = (ArrayList) result.get("list");
//                                if(arr.size() > 0 ) {
//                                    for(Map itemMap : arr) {
//                                        FilmModel film = new FilmModel();
//                                        film.id = String.format("%d",itemMap.get("id"));
//                                        film.title = (String)itemMap.get("title");
//                                        film.authorName = (String)itemMap.get("author");
//                                        film.moviePath = (String)itemMap.get("fileUrl");
//                                        film.imagePath = (String)itemMap.get("posterUrl");
//                                        film.checkResult = (String)itemMap.get("auditInfo");
//                                        film.checkTime = (String)itemMap.get("auditTime");
//                                        film.isRemote = true;
//                                        if(fstate == -1) {
//                                            film.isPassed = false;
//                                        }
//                                        else {
//                                            film.isPassed = true;
//                                        }
//                                        fmarr_operating.add(film);
//
//                                    }
//                                    fmtv_operating.adapter().notifyDataSetChanged();
//                                    fmtv_operating.finishLoadMore(true,false,true);
//                                }
//                                else {
//                                    fmtv_operating.finishLoadMore(false,true,true);
//                                }
//                                return;
//                            }
//                        }
//                        fmtv_operating.finishLoadMore(false,true,true);
//                        //请求失败.
//                        Toast.makeText(UploadedFilmActivity.this, "请求失败,请检查网络", Toast.LENGTH_LONG).show();
//                    }
//                });
//            }
//        });
    }

    public void onClickBack(View view) {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        loadFilmsFromLightStore();
    }

    //这个算应用间的
    public void onClickUpload(View view) {
//        Intent intent = new Intent(com.vgemv.vgemvugc.module.uploadedFilm.UploadedFilmActivity.this, LocalFilmActivity.class);
//        startActivity(intent);
//        //设置路径
//        String filmDir = GlobalModel.sharedInstance.mstr_filmDirPath;
//
//        // 选择视频
//        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//
//        //设置类型
////        intent.setType("video/*");
////        intent.setType("*/*");
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            //authority为前面配置的字符串
//            //设置文件路径没用.
//            Uri contentUri = null;
//            try {
//                contentUri = FileProvider.getUriForFile(this, "com.vgemv.ugcdemo.fileprovider", new File(filmDir));
//            }
//            catch(Exception e) {
//                e.printStackTrace();
//            }
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            intent.putExtra("oneshot", 0);
//            intent.putExtra("configchange", 0);
//
//            intent.setDataAndType(contentUri,"video/*");
//
////            intent.addCategory(Intent.CATEGORY_OPENABLE);
//        }
//        else {
//            File file = new File(filmDir);
//            intent.setData(Uri.fromFile(file.getParentFile()));
//        }
////        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
////        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivityForResult(intent, ChooseFilm);
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode == ChooseFilm) {
//
//
//            if(data == null) {
//                return;
//            }
//            Uri uri = data.getData();
//        String videoUrl = getRealPath(uri);
//
//
//            UploadedFilmListFragment.FilmModel film = new UploadedFilmListFragment.FilmModel();
//            film.filePath = videoUrl;
//            film.fileName = videoUrl.substring(videoUrl.lastIndexOf("\\") + 1);
//
//
//            boolean isUploaded = true;
//
//            SharedPreferences sp = getSharedPreferences(SharedPreferenceKey,MODE_PRIVATE);
//            HashSet<String> set = (HashSet<String>) sp.getStringSet(UploadedFilmKey,null);
//            if(set == null){
//                isUploaded = false;
//            }
//            else {//默认的StringSet会进行内容的对比.
//                String str_json = mGson.toJson(film);
//                isUploaded = set.contains(str_json);
//            }
//
//
//        //判断是否重复,重复不上传,给出提示
//            if(isUploaded) {
//                Toast.makeText(this,"该文件已经上传",Toast.LENGTH_SHORT).show();
//            }
//            else {
//                uploadFilmAndStore(film);
//            }
//        }
//    }

//    public void storeToLightStore(UploadedFilmListFragment.FilmModel film) {
//        Gson gson = new Gson();
//        String str_json = gson.toJson(film);
//        SharedPreferences sp = getSharedPreferences(SharedPreferenceKey,MODE_PRIVATE);
//        HashSet<String> set = (HashSet<String>) sp.getStringSet(UploadedFilmKey,new HashSet<String>());
//        set.add(str_json);
//
//        SharedPreferences.Editor editor = sp.edit();
//        editor.putStringSet(UploadedFilmKey,set);
//        editor.commit();
//
//    }

//    public void uploadFilmAndStore(UploadedFilmListFragment.FilmModel model) {
//
//        String filePath = model.filePath;
//        File file = new File(filePath);
//        FileInputStream fis = null;
//        int fileSize = 0;
//        try {
//            fis = new FileInputStream(file);
//            fileSize = fis.available();
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        Gson gson = new Gson();
//        String str_tkUrl = "http://169.vgemv.com:59091/live_upload//tk";
//        String str_fileSize = String.format("%d",fileSize);
//
//        Map map_tkParam = new HashMap();
//        map_tkParam.put("name","1.mp4");
//        map_tkParam.put("type","video/mp4");
//        map_tkParam.put("size",str_fileSize);
//        map_tkParam.put("modified","js888");
//        map_tkParam.put("92330","");
//
//
//        GajOhNetworkManager.sharedInstance().getRequest(str_tkUrl,map_tkParam , new IGajNetworkCallback() {
//            @Override
//            public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//                if(!isSuccess) {
//                    JsThreadManager.runInMainThread(0, true, new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(UploadedFilmActivity.this,"请求失败,请检查网络",Toast.LENGTH_LONG).show();
//                        }
//                    });
//                    return;
//                }
//                Map<String,String> map = gson.fromJson(result,Map.class);
//
//
//                String str_newFileStreamUpload1 = "http://169.vgemv.com:59091/live_upload//newFileStreamUpload";
//                String token = map.get("token");
//                Map map_upload1Param = new HashMap();
//                map_upload1Param.put("type","1");
//                map_upload1Param.put("token",token);
//                map_upload1Param.put("client","android");
//                map_upload1Param.put("name","1.mp4");
//                map_upload1Param.put("size",str_fileSize);
//                map_upload1Param.put("92273","");
//
//
//
//
//                GajOhNetworkManager.sharedInstance().getRequest(str_newFileStreamUpload1,map_upload1Param , new IGajNetworkCallback() {
//                    @Override
//                    public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//                        if(!isSuccess) {
//                            JsThreadManager.runInMainThread(0, true, new Runnable() {
//                                @Override
//                                public void run() {
//                                    Toast.makeText(UploadedFilmActivity.this,"请求失败,请检查网络",Toast.LENGTH_LONG).show();
//                                }
//                            });
//                            return;
//                        }
//
//
//
//                        GajOhNetworkManager.sharedInstance().uploadFile(str_newFileStreamUpload1, map_upload1Param, model.filePath, model.fileName, str_fileSize, null, new IGajNetworkCallback() {
//                            @Override
//                            public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//                                if(!isSuccess) {
//                                    JsThreadManager.runInMainThread(0, true, new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            Toast.makeText(UploadedFilmActivity.this,"请求失败,请检查网络",Toast.LENGTH_LONG).show();
//                                        }
//                                    });
//                                    return;
//                                }
//                                JsThreadManager.runInMainThread(0, true, new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(UploadedFilmActivity.this,"上传成功",Toast.LENGTH_LONG).show();
//                                        storeToLightStore(model);
//                                        loadFilmsFromLightStore();
//                                        Log.d("xxx","Edit");
//                                    }
//                                });
//
//                            }
//                        });
//
//                    }
//                });
//            }
//        });
//
//
//    }
//
//
//
//
//
//
//    public void loadFilmsFromLightStore() {
//        SharedPreferences sp = getSharedPreferences(SharedPreferenceKey,MODE_PRIVATE);
//        HashSet<String> set = (HashSet<String>) sp.getStringSet(UploadedFilmKey,null);
//        mal.clear();
//        if(set != null) {
//            //必须是同一个迭代器,不能反复获取.
//            Iterator<String> iterator = set.iterator();
//            while(iterator.hasNext()){
//                String str_json = iterator.next();
//                UploadedFilmListFragment.FilmModel film = mGson.fromJson(str_json, UploadedFilmListFragment.FilmModel.class);
//                mal.add(film);
//            }
//        }
//        mtv_checked.refresh();
//    }
//
//    void loadFilmsFromDir() {
//
//        String dir = GajFileManager.appDirPathInPubDoc();
//        String filmDir = dir+"/film";
//        ///storage/emulated/0/Documents/GajDemo/film
//        Log.d("xxx",String.format("Film dir is %s",filmDir));
//        //创建filmDir
//        File file_filmDir = new File(filmDir);
//        if(!file_filmDir.exists()) {
//            try {
//                file_filmDir.mkdir();
//            }
//            catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
////        File[] files = file_filmDir.listFiles()
////        都是相对路径.
//       String[] filePaths = file_filmDir.list();
//        mal.clear();
//        if(filePaths != null) {
//            for (String filePath : filePaths) {
//                UploadedFilmListFragment.FilmModel model = new UploadedFilmListFragment.FilmModel();
//                model.isUploaded = false;
//                model.filePath = String.format("%s/%s",filmDir,filePath);
//                model.fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
//                mal.add(model);
//            }
//        }

// substring(filePath.lastIndexOf("\\") + 1);

//        String fileName = "1.mp4";
//        String filePath = filmDir + fileName;
//        //读取指定目录下的视频文件.
//        ArrayList<FilmListFragment.FilmModel> models = new ArrayList<FilmListFragment.FilmModel>();
//
//        FilmListFragment.FilmModel model1 = new FilmListFragment.FilmModel();
//        model1.isUploaded = false;
//        model1.filePath = filePath;
//        model1.fileName = fileName;
//        models.add(model1);
//
//        FilmListFragment.FilmModel model2 = new FilmListFragment.FilmModel();
//        model2.isUploaded = false;
//        model2.filePath = filePath;
//        model2.fileName = fileName;
//        models.add(model2);
//
//        FilmListFragment.FilmModel model3 = new FilmListFragment.FilmModel();
//        model3.isUploaded = false;
//        model3.filePath = filePath;
//        model3.fileName = fileName;
//        models.add(model3);
//
//        mal = models;
//    }

//    private String getRealPath(Uri uri) {
//
//        String realPath = null;
//
//        if (Build.VERSION.SDK_INT < 11)
//            realPath = FileHelper2.getRealPathFromURI_BelowAPI11(this, uri);
//
//            // SDK >= 11
//        else
//            realPath = FileHelper2.getRealPathFromURI_API11_And_Above(this, uri);
//
//        return realPath;
//    }
}
